﻿using System;
using System.Collections.Generic;
using System.Web;
using YawetagDemo.Framework.Data;
using YawetagDemo.Framework.Services;
using YawetagDemo.WebUI.Models;
using YawetagLib.System.Facade.Services;
using YawetagLib.System.Services;
using YawetagLib.System.Web.Services;
using System.Linq;

namespace YawetagDemo.WebUI.Services
{
    internal class Facade : WebFacade
    {
        private IArtist _lisa;
        private IArtist _brendan;
        private IBand _dcd;

        private IAlbum _dcda;
        private IAlbum _si;
        private IAlbum _wrds;
        private IAlbum _se;
        private IAlbum _aion;
        private IAlbum _il;
        private IAlbum _sc;

        private IPerson _jrgoe;
        private IPerson _sarofe;

        public Facade(IFacadeFactory facadeFactory, IServiceFactory serviceBroker, HttpContextBase httpContext) 
            : base(facadeFactory, serviceBroker, httpContext)
        {
        }

        public IList<ArtistModel> GetArtistModels()
        {
            return ServiceBroker.Get<IMusicService>().GetArtists().Select(i => new ArtistModel(i)).ToList();
        }

        public IList<ArtistModel> GetLisaAndBrendanArtistModels()
        {
            IMusicService musicService = ServiceBroker.Get<IMusicService>();

            return new[]
            {
                new ArtistModel(musicService.GetArtist(_lisa.ArtistID)), 
                new ArtistModel(musicService.GetArtist(_brendan.ArtistID))
            }.ToList();
        }

        public void LogArtistsOntoServer()
        {
            ServiceBroker.Get<IMusicService>().LogArtists(GetArtist);
        }

        private static string GetArtist(IArtist artist)
        {
            ArtistModel model = new ArtistModel(artist);

            return string.Format("  {0} - Age: {1} - Bands: {2}", model.Name, model.Age, model.Bands);
        }

        public IList<AlbumRankingItemModel> GetAlbumRankingItemModels(string filterCriteria, string sortExpression,
            int startRowIndex, int? maximumRows)
        {
            return ServiceBroker.Get<IMusicService>()
                .GetAlbumRankingItems(filterCriteria, sortExpression, startRowIndex, maximumRows).ResultSet
                .Select(i => new AlbumRankingItemModel(i))
                .ToList();
        }

        public void CreateEntities()
        {
            ICoreService coreService = ServiceBroker.Get<ICoreService>();
            IMusicService musicService = ServiceBroker.Get<IMusicService>();

            _lisa = musicService.CreateArtist("Lissa Gerrard", new DateTime(1900, 1, 1));
            _brendan = musicService.CreateArtist("Brendan Perry", new DateTime(1959, 6, 30));
            _dcd = musicService.CreateBand("Dead Can Dancesss");
            musicService.AddArtistToBand(_dcd.BandID, _lisa.ArtistID);
            musicService.AddArtistToBand(_dcd.BandID, _brendan.ArtistID);

            _dcda = musicService.CreateAlbum(_dcd.BandID, "Dead Can Dance", new DateTime(1984, 2, 27));
            _si = musicService.CreateAlbum(_dcd.BandID, "Spleen and Ideal", new DateTime(1985, 11, 1));
            _wrds = musicService.CreateAlbum(_dcd.BandID, "Within Realm Dying Sun", new DateTime(1987, 7, 27));
            _se = musicService.CreateAlbum(_dcd.BandID, "The Serpent's Egg", new DateTime(1900, 1, 1));
            _aion = musicService.CreateAlbum(_dcd.BandID, "Aion", new DateTime(1990, 6, 11));
            _il = musicService.CreateAlbum(_dcd.BandID, "Into the Labyrinth", new DateTime(1993, 9, 13));
            _sc = musicService.CreateAlbum(_dcd.BandID, "Spiritchaser", new DateTime(1996, 6, 3));

            _jrgoe = coreService.GetPerson(1);
            _sarofe = coreService.GetPerson(2);
            musicService.RankAlbum(_dcda.AlbumID, _jrgoe.PersonID, 3);
            musicService.RankAlbum(_si.AlbumID, _jrgoe.PersonID, 3);
            musicService.RankAlbum(_wrds.AlbumID, _jrgoe.PersonID, 4);
            musicService.RankAlbum(_se.AlbumID, _jrgoe.PersonID, 5);
            musicService.RankAlbum(_aion.AlbumID, _jrgoe.PersonID, 4);
            musicService.RankAlbum(_il.AlbumID, _jrgoe.PersonID, 4);
            musicService.RankAlbum(_sc.AlbumID, _jrgoe.PersonID, 4);
            musicService.RankAlbum(_il.AlbumID, _sarofe.PersonID, 5);
            musicService.RankAlbum(_sc.AlbumID, _sarofe.PersonID, 4);
        }

        public void UpdateEntities()
        {
            IMusicService musicService = ServiceBroker.Get<IMusicService>();

            musicService.UpdateArtist(_lisa.ArtistID, "Lisa Gerrard", new DateTime(1961, 4, 12));
            musicService.UpdateBand(_dcd.BandID, "Dead Can Dance");
            musicService.UpdateAlbum(_wrds.AlbumID, "Within the Realm of a Dying Sun", new DateTime(1987, 7, 27));
            musicService.UpdateAlbum(_se.AlbumID, "The Serpent's Egg", new DateTime(1988, 10, 24));
            musicService.RankAlbum(_wrds.AlbumID, _jrgoe.PersonID, 5);
            musicService.RankAlbum(_wrds.AlbumID, _sarofe.PersonID, 5);
            musicService.RankAlbum(_se.AlbumID, _sarofe.PersonID, 5);
            musicService.RankAlbum(_aion.AlbumID, _sarofe.PersonID, 5);
        }

        public void DeleteEntities()
        {
            IMusicService musicService = ServiceBroker.Get<IMusicService>();

            musicService.DeleteBand(_dcd.BandID);
            musicService.DeleteArtist(_lisa.ArtistID);
            musicService.DeleteArtist(_brendan.ArtistID);
        }
    }
}
